#!/bin/sh

CUR_DIR=$PWD
LIB_DIR=$CUR_DIR/dependencies/libs
BUNDLE_DIR=$CUR_DIR/bin
FRAME_DIR=$CUR_DIR/bin/OpenPool.app/Contents/Frameworks/
EXE_DIR=$CUR_DIR/bin/OpenPool.app/Contents/MacOS/

XCODE_OUT=Release

if [ -d $BUNDLE_DIR/OpenPool.app ]; then
	rm -r $BUNDLE_DIR/OpenPool.app
fi

#if the .xcodeproj file is present assume the compiling is being made trhough xcode
#in this case, the bundle will either be in ./Debug or ./Release
#if not, then standard makefiles are being used and it will be in ./
if [ -d $CUR_DIR/OPENPOOL.xcodeproj ]; then	
	cp -r $CUR_DIR/$XCODE_OUT/OpenPool.app $BUNDLE_DIR
else
	cp -r $CUR_DIR/OpenPool.app $BUNDLE_DIR
fi

mkdir $FRAME_DIR

cp $LIB_DIR/libfreeimage-3.13.1.dylib $FRAME_DIR
cp $LIB_DIR/libphysfs.2.0.2.dylib $FRAME_DIR

cp -r $LIB_DIR/sfml-graphics.framework $FRAME_DIR
cp -r $LIB_DIR/sfml-window.framework $FRAME_DIR
cp -r $LIB_DIR/sfml-system.framework $FRAME_DIR
cp -r $LIB_DIR/sfml-audio.framework $FRAME_DIR
cp -r $LIB_DIR/sfml-network.framework $FRAME_DIR
cp -r $LIB_DIR/sndfile.framework $FRAME_DIR

cd $EXE_DIR

install_name_tool -change libfreeimage-3.13.1.dylib @executable_path/../Frameworks/libfreeimage-3.13.1.dylib OpenPool
install_name_tool -change libphysfs.2.0.2.dylib @executable_path/../Frameworks/libphysfs.2.0.2.dylib OpenPool

cd $CUR_DIR
